package com.jd.platform.sample2.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.jd.platform.sample2.entity.properties.Redis2Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * @author wuweifeng wrote on 2017/10/27.
 */
@Configuration
public class Redis2Config {
    @Resource
    private Redis2Properties redis2Properties;

    /**
     * 第二个Redis配置
     */
    @Bean(name = "redis2ConnectionFactory")
    public RedisConnectionFactory redis2ConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(redis2Properties.getHost());
        config.setPort(redis2Properties.getPort());
        config.setPassword(RedisPassword.of(redis2Properties.getPassword()));
        config.setDatabase(redis2Properties.getDatabase());
        return new JedisConnectionFactory(config);
    }

    @Bean(name = {"redis2Template", "stringRedis2Template"})
    public StringRedisTemplate redis2Template() {
//        RedisTemplate<String, Object> redis2Template = new RedisTemplate<>();
        StringRedisTemplate redis2Template = new StringRedisTemplate();

        redis2Template.setConnectionFactory(redis2ConnectionFactory()); //设置redis的连接工厂对象
        redis2Template.setKeySerializer(new StringRedisSerializer()); //设置redis key的序列化器
        redis2Template.setValueSerializer(new StringRedisSerializer()); //设置redis value的序列化器
//        redis2Template.setHashKeySerializer(new StringRedisSerializer()); //设置redis hash value序列化
//        redis2Template.setHashValueSerializer(redisSerializer()); //设置redis hash value序列化
//        redis2Template.afterPropertiesSet(); //初始化时的逻辑
        return redis2Template;
    }

    @Bean
    public RedisMessageListenerContainer container(ShadowKeyExpirationListener listener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redis2ConnectionFactory());

        // 订阅所有key的过期事件（pattern为__keyevent@*__:expired）
        container.addMessageListener(listener, new PatternTopic("__keyevent@*__:expired"));
        return container;
    }



//    public RedisSerializer<Object> redisSerializer() {
//        //创建JSON序列化器
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        //必须设置，否则无法序列化实体类对象
//        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
//        return new GenericJackson2JsonRedisSerializer(objectMapper);
//    }
}
